package com.ruiyin.service;

import com.ruiyin.dao.ValidDao;
import com.ruiyin.entity.App;
import com.ruiyin.entity.BaseEntity;
import com.ruiyin.entity.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
@Service
public class ValidService extends BaseService {
    private static Integer NO = 0;//0激活
    private static Integer DIS = 0;//账号正常
    private static Integer FLAG = 1;//非终身


    @Autowired
    private ValidDao dao;

    public void insertAccount(Valid va) {
        if (!queryAccount(va)) {
            return;//说明数据库中存在
        }
        va.setCreateDate(getDate());
        va.setId(getID());
        va.setType(NO);//0是设置
        dao.insert(va);
    }

    public int query(Valid valid) {
        Valid account = dao.findAccount(valid);
        if (StringUtils.isEmpty(account)) {//用户不存在
            return 1001;
        }

        //0未设置 1测试卡 2周卡 3月卡 4年卡
        if (account.getStatus() == 1) {//禁用
            return -1;
        }
        if (account.getType() == 0) {//未激活
            return 0;
        }
        if (account.getType() == 1) {//测试卡
            return 1;
        }
        if (account.getType() == 2) {//周卡
            return 2;
        }
        if (account.getType() == 3) {//月卡
            return 3;
        }
        if (account.getType() == 4) {//终身
            return 4;
        }

        return -1;
    }

    public boolean queryAccount(Valid valid) {
        return dao.query(valid) == 0 ? true : false;
    }

    public Valid findStutas(Valid valid) {
        Valid va = dao.queryStatus(valid);
        if(!org.apache.commons.lang3.StringUtils.isEmpty(va.getDateTime())){
            return va;
        }

        return null;
    }

    public Object queryVersion() {
        return dao.queryVersion();
    }


    @Scheduled(cron = "0/59 * * * * ? ")
    @Transactional(rollbackFor = Exception.class, readOnly = false)
    public void queryDate() {
        List<BaseEntity> entity = dao.queryDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < entity.size(); i++) {
            if (org.apache.commons.lang3.StringUtils.isNotEmpty(entity.get(i).getDateTime())) {
                try {
                    Date end = sdf.parse(entity.get(i).getDateTime());
                    Date start = sdf.parse(getDate());
                    if (end.before(start)) {
                        dao.changeType(entity.get(i).getImei());
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }
        }
    }

    @Scheduled(cron = "0/59 * * * * ? ")
    @Transactional(rollbackFor = Exception.class, readOnly = false)
    public void clearAccount(){

    }
}
